<?php
/**
 * Ecart
 *
 * This file is part of Ecart.
 *
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ecart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category    Ecart
 * @package     Ecart_Admin
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */

/**
 *
 * @category    Ecart
 * @package     Ecart_Admin
 * @subpackage  Controller
 * @author      Ecart Core Team <core@ecartcommerce.com>
 */
class Ecart_Admin_SiteController extends Ecart_Admin_Controller_Back
{
    protected $_table;

    public function init()
    {
        parent::init();
        $this->setActionToAclAssignment(array(
                'index' => 'admin/site/view',
                'edit'  => 'admin/site/edit',
                'save'  => 'admin/site/edit'
        ));
        $this->_table = Ecart::single('core/site');
    }

    public function indexAction()
    {
        $this->view->pageTitle = Ecart::translate('core')->__('Sites');
        $this->view->sites = $this->_table->fetchAll()->toArray();
        $this->render();
    }

    public function getListAction()
    {
        $this->_helper->json->sendSuccess(array(
            'data' => Ecart::single('core/site')->getList()
        ));
    }

    public function saveAction()
    {
        $this->_helper->layout->disableLayout();

        $data = Zend_Json_Decoder::decode($this->_getParam('data'));

        $this->_table->save($data);
        Ecart::message()->addSuccess(
            Ecart::translate('admin')->__(
                'Site was saved successfully'
            )
        );
        return $this->_helper->json->sendSuccess();
    }

    public function deleteAction()
    {
        $this->_helper->layout->disableLayout();

        $data = Zend_Json_Decoder::decode($this->_getParam('data'));

        $this->_table->delete(
            $this->db->quoteInto('id IN(?)', $data)
        );
        Ecart::dispatch('core_site_delete_success', array('site_ids' => $data));
        Ecart::message()->addSuccess(
            Ecart::translate('admin')->__(
                'Site was deleted successfully'
            )
        );
        return $this->_helper->json->sendSuccess();
    }
}